

/**
 * Draws the sundial and the gnomen
 * 
 * @author Wendi Fujino
 * March 2013
 */

import java.awt.*;
import javax.swing.*;

import calculationsandtests.SundialCalc;

public class Drawing extends JPanel
{
	SundialCalc calc = new SundialCalc();
	MarsCalc marsCalc = new MarsCalc();
	private boolean earthDial = true;
	private double angle;
	private double sevenFive;
	private double eightFour;
	private double nineThree;
	private double tenTwo;
	private double elevenOne;
	
   
	public void paintComponent (Graphics g)
	{
		int x = 0;
		int h = 0;
		double slope = 0;
		int x2 = 0;
		int y1 = 0;
		if(earthDial){
			angle = calc.getGnomon();
			sevenFive = 90 - calc.getSevenFive();
			eightFour = 90 - calc.getEightFour();
			nineThree = 90 - calc.getNineThree();
			tenTwo = 90 - calc.getTenTwo();
			elevenOne = 90 - calc.getElevenOne();
		}else{
			angle = marsCalc.getGnomon();
			sevenFive = 90 - marsCalc.getSevenFive();
			eightFour = 90 - marsCalc.getEightFour();
			nineThree = 90 - marsCalc.getNineThree();
			tenTwo = 90 - marsCalc.getTenTwo();
			elevenOne = 90 - marsCalc.getElevenOne();
		}
		
		
		//sets up the background color
		super.paintComponent(g);
		this.setBackground(Color.WHITE);
		
		//creates the rectangle of the sundial
		g.setColor(Color.BLACK);
		g.drawRect(15, 15, 480, 310);
		
		//labels the title
  	 g.setColor(Color.BLACK);
  	 g.drawString("Horizontal Sundial For " + angle + " degrees North Latitude", 95, 60);
		
		if(earthDial){
  			//labels Noon, AM, and PM
  			g.setColor(Color.BLACK);
  			g.drawString(calc.getNoon(), 250, 110);
  			g.setColor(Color.BLACK);
  			g.drawString(calc.getAMPM() + " AM", 22, 300);
  			g.setColor(Color.BLACK);
  			g.drawString(calc.getAMPM() + " PM", 439, 300);
  		}else{
  		  //labels Noon, AM, and PM
  	  	 g.setColor(Color.BLACK);
  	  	 g.drawString(marsCalc.getNoon(), 250, 110);
  			g.setColor(Color.BLACK);
  			g.drawString(marsCalc.getAMPM() + " AM", 22, 300);
  			g.setColor(Color.BLACK);
  			g.drawString(marsCalc.getAMPM() + " PM", 439, 300);
  		}
		
		//creates the gnomon
		if (angle == 90)
		{
			//makes a rectangle
			g.setColor(Color.BLACK);
			g.drawRect(540, 160, 200, 140);
		}
		else
		{
			//vertical line
			g.setColor(Color.BLACK);
			g.drawLine(540, 160, 540, 300);
			
			//horizontal line
			x = (int) (140 * Math.tan(Math.toRadians(angle)));
			
			if(x > 346)
			{
				g.setColor(Color.BLACK);
				g.drawLine(540, 160, (346 + 540), 160);
			}
			else
			{
				g.setColor(Color.BLACK);
				g.drawLine(540, 160, (x + 540), 160);
			}
			
			//hypotenuse line
			slope = (double) ((300) - (160)) / (540 - (x + 540));
			y1 = (int) Math.round(300 - (slope * (540 - ((346 + 540)))));
			if (x > 346)
			{
				g.setColor(Color.BLACK);
				g.drawLine((346 + 540), y1, 540, 300);
			}
			else
			{
				g.setColor(Color.BLACK);
				g.drawLine((x + 540), 160, 540, 300);
			}
		}
		
		//creates the 90 degree hour line
		g.setColor(Color.BLACK);
		g.drawLine(265, 130, 265, 300);
		
		//creates the 0 degree hour line
		g.setColor(Color.BLACK);
		g.drawLine(75, 300, 435, 300);
		
		//creates hour lines right side
		//5PM
		h = (int) Math.round(170 * Math.tan(Math.toRadians(sevenFive)));
		slope = (double) ((300) - (300 - h)) / (265 - 435);
		x2 = (int) Math.round(((130 - 300 + (slope * 265)) / slope));
		if (x2 > 435)
		{
			g.setColor(Color.BLACK);
			g.drawLine(265, 300, 435, (300 -h));
		}
		else
		{
			g.setColor(Color.BLACK);
			g.drawLine(265, 300, x2, 130);
		}
		//4PM
		h = (int) Math.round(170 * Math.tan(Math.toRadians(eightFour)));
		slope = (double) ((300) - (300 - h)) / (265 - 435);
		x2 = (int) Math.round(((130 - 300 + (slope * 265)) / slope));
		if (x2 > 435)
		{
			g.setColor(Color.BLACK);
			g.drawLine(265, 300, 435, (300 -h));
			g.drawString(calc.getAMPMAdjusted(-2) + "PM", 435, (300 -h));
		}
		else
		{
			g.setColor(Color.BLACK);
			g.drawLine(265, 300, x2, 130);
			g.drawString(calc.getAMPMAdjusted(-2) + "PM", x2, 130);
		}
		//3PM
		h = (int) Math.round(170 * Math.tan(Math.toRadians(nineThree)));
		slope = (double) ((300) - (300 - h)) / (265 - 435);
		x2 = (int) Math.round(((130 - 300 + (slope * 265)) / slope));
		g.setColor(Color.BLACK);
		g.drawLine(265, 300, x2, 130);
		//2PM
		h = (int) Math.round(170 * Math.tan(Math.toRadians(tenTwo)));
		slope = (double) ((300) - (300 - h)) / (265 - 435);
		x2 = (int) Math.round(((130 - 300 + (slope * 265)) / slope));
		g.setColor(Color.BLACK);
		g.drawLine(265, 300, x2, 130);
		g.drawString(calc.getAMPMAdjusted(-4) + "PM", x2, 130);
		//1PM
		h = (int) Math.round(170 * Math.tan(Math.toRadians(elevenOne)));
		slope = (double) ((300) - (300 - h)) / (265 - 435);
		x2 = (int) Math.round(((130 - 300 + (slope * 265)) / slope));
		g.setColor(Color.BLACK);
		g.drawLine(265, 300, x2, 130);
		
		//creates hour lines left side
		//7AM
		h = (int) Math.round(170 * Math.tan(Math.toRadians(sevenFive)));
		slope = (double) ((300) - (300 - h)) / (265 - 75);
		x2 = (int) Math.round(((130 - 300 + (slope * 265)) / slope));
		if (x2 < 75)
		{
			g.setColor(Color.BLACK);
			g.drawLine(265, 300, 75, (300 -h));
		}
		else
		{
			g.setColor(Color.BLACK);
			g.drawLine(265, 300, x2, 130);
		}
		//8AM
		h = (int) Math.round(170 * Math.tan(Math.toRadians(eightFour)));
		slope = (double) ((300) - (300 - h)) / (265 - 75);
		x2 = (int) Math.round(((130 - 300 + (slope * 265)) / slope));
		if (x2 < 75)
		{
			g.setColor(Color.BLACK);
			g.drawLine(265, 300, 75, (300 -h));
			g.drawString(calc.getAMPMAdjusted(2) + "AM", 75, (300 -h));
		}
		else
		{
			g.setColor(Color.BLACK);
			g.drawLine(265, 300, x2, 130);
			g.drawString(calc.getAMPMAdjusted(2) + "AM", x2, 130);
		}
		
		//9AM
		h = (int) Math.round(170 * Math.tan(Math.toRadians(nineThree)));
		slope = (double) ((300) - (300 - h)) / (265 - 75);
		x2 = (int) Math.round(((130 - 300 + (slope * 265)) / slope));
		g.setColor(Color.BLACK);
		g.drawLine(265, 300, x2, 130);
		
		//10AM
		h = (int) Math.round(170 * Math.tan(Math.toRadians(tenTwo)));
		slope = (double) ((300) - (300 - h)) / (265 - 75);
		x2 = (int) Math.round(((130 - 300 + (slope * 265)) / slope));
		g.setColor(Color.BLACK);
		g.drawLine(265, 300, x2, 130);
		g.drawString(calc.getAMPMAdjusted(4) + "AM", x2, 130);
		
		//11AM
		h = (int) Math.round(170 * Math.tan(Math.toRadians(elevenOne)));
		slope = (double) ((300) - (300 - h)) / (265 - 75);
		x2 = (int) Math.round(((130 - 300 + (slope * 265)) / slope));
		g.setColor(Color.BLACK);
		g.drawLine(265, 300, x2, 130);
	}
	
	public void sendLocation(double longitude, double latitude, int month, int day, int year, boolean daySave)
	{
		//sets the variables in the SundialCalc object
		calc.location(longitude, latitude, month, day, year, daySave);
	}
   
   public void sendMarsLocation(double longitude, double latitude, int month, int day, int year){
		marsCalc.location(longitude, latitude, year, month, day);
		this.earthDial = false;
	}
}
